﻿using System;
using System.Collections.Generic;
using Common;
using System.Web.Script.Serialization;
using Educational.Business;
using Basic.Business;
using Educational.VO;

public partial class Educational_Ajax_PositionManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //强制刷新页面
        Response.Buffer = true;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        Response.AppendHeader("Pragma", "No-Cache");

        JavaScriptSerializer json = new JavaScriptSerializer();
        if (!String.IsNullOrEmpty(Request.QueryString["Action"]))
        {
            string action = BadChar.ReplaceBadChar(Request.QueryString["Action"]);
            switch (action)
            {
                #region deletePosition删除职位
                case "deletePosition":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            int positionId = int.Parse(Request.QueryString["positionId"]);
                            PositionAdmin.DeletePosition(school.schoolId, positionId);
                            result.error = false;
                            result.msg = "删除职位成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region saveUpdatePosition保存职位修改
                case "saveUpdatePosition":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            int positionId = int.Parse(Request.Form["positionId"]);
                            string positionName = BadChar.ReplaceBadChar(Request.Form["positionName"].Trim());
                            if (positionName.Equals(""))
                                throw new Exception("职位名称不能为空");
                            int departmentId = int.Parse(Request.Form["departmentId"]);
                            int schoolRoleGroupId = int.Parse(Request.Form["schoolRoleGroupId"]);
                            PositionAdmin.UpdatePosition(positionId, school.schoolId, departmentId, schoolRoleGroupId, positionName);
                            result.error = false;
                            result.msg = "修改职位成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region saveAddPosition保存职位添加
                case "saveAddPosition":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            string positionName = BadChar.ReplaceBadChar(Request.Form["positionName"].Trim());
                            if (positionName.Equals(""))
                                throw new Exception("职位名称不能为空");
                            int departmentId = int.Parse(Request.Form["departmentId"]);
                            int schoolRoleGroupId = int.Parse(Request.Form["schoolRoleGroupId"]);
                            PositionAdmin.AddPosition(school.schoolId, departmentId, schoolRoleGroupId, positionName);
                            result.error = false;
                            result.msg = "添加职位成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region search查询列表
                case "search":
                    {
                        Result<Position> result = new Result<Position>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            int page = int.Parse(Request["page"]);
                            int perPage = int.Parse(Request["perpage"]);
                            int departmentId = int.Parse(Request.QueryString["departmentId"]);
                            School school = (School)Session["school"];
                            PageResult<Position> pageResult = PositionAdmin.GetsPosition(school.schoolId,departmentId, (page - 1) * perPage, page * perPage);
                            result.data = pageResult.data;
                            result.total = pageResult.totalCount;
                            result.page = page;
                            result.error = false;
                            result.msg = "查询职位列表成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                default:
                    {
                        Result<object> result = new Result<object>();
                        result.error = true;
                        result.msg = StringTable.ErrorUnknowAction;
                        Response.Write(json.Serialize(result));
                    }
                    break;
            }
        }
        else
        {
            Result<object> result = new Result<object>();
            result.error = true;
            result.msg = StringTable.ErrorMissingParameters;
            Response.Write(json.Serialize(result));
        }
        Response.End();
    }
}
